package cetcbigdata.da.service.hainan;

import cetcbigdata.da.base.Base;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

import static cetcbigdata.da.service.hainan.util.ServiceHNUtil.getSimpleBasicInfo;

/**
 * 政务公开
 * 监管清单
 */

public class JianGuanList extends Base {
    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {

    }

    @Override
    public JSONObject getDataDemo() {
        try{
            return parseListPage();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    private JSONObject parseListPage() {
        //详情页
        int currentPage = 1;
        //总页数
        int pages = 0;
        while (currentPage != pages){
            String listUrl = "https://wssp.hainan.gov.cn/wssp3/supervision/supervisionList";
            JSONObject listHeader = new JSONObject();
            listHeader.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");
            listHeader.put("Referer", "https://wssp.hainan.gov.cn/hnwt/jianguan_detail?qdCategory=jgqd&title=%E7%9B%91%E7%AE%A1%E6%B8%85%E5%8D%95");
            JSONObject listParmas = new JSONObject();
            listParmas.put("dept","");
            listParmas.put("regionId","HZ2881f4424539dd0142453c856b0025");
            listParmas.put("taskType","");
            listParmas.put("taskName","");
            listParmas.put("pageNumber",currentPage);
            listParmas.put("pageSize","10");

            String listStrResponse = postFormReq(listUrl,listHeader,listParmas);
            JSONObject listResponse = JSONObject.parseObject(listStrResponse);
            JSONArray datas = listResponse.getJSONObject("data").getJSONArray("datas");
            for (Object o : datas){
                JSONObject data = (JSONObject) o;
                JSONArray basicList = data.getJSONArray("basicList");
                JSONObject result = new JSONObject();
                JSONArray rseultList = new JSONArray();
                String url = "https://wssp.hainan.gov.cn/wssp3/supervision/supervisionDetails";
                String strResponse = null;
                for (Object o1 : basicList){
                    JSONObject basic = (JSONObject) o1;
                    String cdId = basic.getString("cdId");
                    String deptId = basic.getString("deptId");

                    JSONObject header = new JSONObject();
                    JSONObject parmas = new JSONObject();
                    header.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");
                    header.put("Referer", "https://wssp.hainan.gov.cn/hnwt/jianguan_content?cdId="+cdId+"Id="+deptId);
                    parmas.put("cdId",cdId);
                    parmas.put("deptId",deptId);

                    strResponse = postFormReq(url,header,parmas);
                    JSONObject response = JSONObject.parseObject(strResponse);
                    JSONObject PageResult = getSimpleBasicInfo(response.getJSONObject("data"));
                    PageResult.put("地址", "https://wssp.hainan.gov.cn/hnwt/jianguan_content?cdId="+cdId+"&deptId="+deptId);
                    rseultList.add(PageResult);
                }
                result.put("名称", data.getString("basicListName"));
                result.put("选项", rseultList);
                System.out.println(currentPage+"\n"+result);//测试用代码
                return processResult(
                        null,
                        null,
                        null,
                        strResponse,
                        result,
                        "海南省",
                        "政务公开-监管清单"
                );
            }
            //更新当前页和总页数
            currentPage++;
            pages = listResponse.getJSONObject("data").getJSONObject("page").getInteger("pageCount") + 1;
        }
        return null;
    }

    public static void main(String[] args) {
        JianGuanList jianGuanList = new JianGuanList();
        jianGuanList.getDataDemo();
    }
}
